<?php
//php观察者模式2
//被观察对象接口
abstract class EventGenerator {
    private $observers = array();
    //添加
    function addObserver(Observer $observer)
    {
        $this->observers[] = $observer;
    }
	//通知
    function notify()
    {
        foreach($this->observers as $observer)
        {
            $observer->update();
        }
    }

}
//观察者接口
interface Observer
{
    function update($event_info = null);
}


class Event extends EventGenerator
{
    function trigger()
    {
        echo "Event<br/>\n";
        $this->notify();
    }
}

class Observer1 implements Observer
{
    function update($event_info = null)
    {
        echo "逻辑1<br />\n";
    }
}

class Observer2 implements Observer
{
    function update($event_info = null)
    {
        echo "逻辑2<br />\n";
    }
}

$event = new Event;
//$event->addObserver(new Observer1);
$event->addObserver(new Observer2);
$event->trigger();